<template>
    <div class="box">
        <div class="outer">
            <el-page-header @back="goBack" content="注册用户"></el-page-header>
            <div class="blank"></div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                class="demo-ruleForm">
                <el-form-item label="用户名" prop="age">
                    <el-input v-model.number="ruleForm.age"></el-input>
                </el-form-item>
                <div class="blank"></div>
                <el-form-item label="密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                </el-form-item>
                <div class="blank"></div>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                <div class="blank"></div>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>

import { FormItem, Form, Input, Button, PageHeader } from 'element-ui'
import request from '../../api/request';
export default {
    data() {
        var checkAge = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('用户名不能为空'));
            }
            setTimeout(() => {
                callback();
                // if (!Number.isInteger(value)) {
                //     callback(new Error('请输入数字值'));
                // } else {
                //     if (value < 18) {
                //         callback(new Error('必须年满18岁'));
                //     } else {
                //         callback();
                //     }
                // }
            }, 1000);
        };
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                pass: '',
                checkPass: '',
                age: ''
            },
            rules: {
                pass: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                age: [
                    { validator: checkAge, trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        submitForm(formName) {
            const user_info = {
                user_name: this.ruleForm.age,
                password: this.ruleForm.pass
            }
            console.log(user_info)
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    await request("post", `/register`, user_info, "form")
                    alert("注册成功")
                    this.$router.push("./Login")
                } else {
                    console.log('输入有误，请重新输入');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        goBack() {
            // console.log('go back');
            this.$router.back()
        },
    },
    components: {
        "el-form": Form,
        "el-form-item": FormItem,
        "el-input": Input,
        "el-button": Button,
        "el-page-header": PageHeader,
    }
}
</script>

<style lang="scss" scoped>
.el-page-header /deep/ .el-page-header__title {
    font-size: 18px;
}

.box {
    background-color: rgb(176, 200, 255);
    height: 100%;
}

.outer {
    margin-top: 200px;
    width: 500px;
    margin: 0 auto;
    background-color: beige;
    position: relative;
    top: 200px;
    padding: 50px 100px 50px 50px;
    border-radius: 15px;
}

.blank {
    height: 50px;
}

.el-form-item /deep/ .el-form-item__label {
    text-align: left;
    font-size: 18px;
}

.el-form-item /deep/ .el-form-item__content {
    display: flex;
    justify-content: center;
}

.is-error /deep/ .el-form-item__error {
    font-size: 18px;
    padding-top: 15px;
}
</style>